package com.bw.common.base

import com.example.common.entity.Res
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

abstract class BaseRepository {


        protected fun <T> safeApiFlow(
            call: suspend () -> Res<T>
        ): Flow<Res<T>> = flow {
            try {
                val response = call()
                emit(response) // 直接发射Res包装的数据
            } catch (e: Exception) {
                emit(Res.error(e.message ?: "Unknown error")) // 错误时返回错误Res=
            }
        }

        // 处理返回List的情况
        protected fun <T> safeApiListFlow(
            call: suspend () -> Res<List<T>>
        ): Flow<Res<List<T>>> = flow {
            try {
                val response = call()
                emit(response)
            } catch (e: Exception) {
                emit(Res.error(e.message ?: "Unknown error", emptyList()))
            }
        }
}
